﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;

namespace BaccaratRally.DataKernel.Service
{
    using BaccaratRally.DataKernel.SqlGateway;
    using BaccaratRally.DataKernel.Entity;

    public class BetItemService : ServiceBase
    {
        public virtual BetItem Create(BetItem newItem)
        {
            return SqlGateway.CreateInstance().Insert<BetItem>(newItem);
        }

        public virtual BetItem Update(BetItem item)
        {
            return SqlGateway.CreateInstance().Update<BetItem>(item);
        }

        public virtual List<BetItem> GetBetItemList(String accountName, int hours)
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("SELECT * FROM Bets WHERE AccountName = '" + accountName + "' and BetTime > DateAdd(hh,-" + hours + ",getdate()) and BetStatus < 9 ");

            return SqlGateway.CreateInstance().Select<BetItem>(sqlstring.ToString(), CommandType.Text).ToList();
        }

        public virtual List<BetItem> NeedSetBetItemList(string accountName, string accountPos)
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("SELECT * FROM Bets WHERE AccountName = '" + accountName + "' and AccountPos = '" + accountPos + "' and BetStatus = 0 and datediff(ss,BetTime,getdate()) < 15 Order By BetID DESC");

            return SqlGateway.CreateInstance().Select<BetItem>(sqlstring.ToString(), CommandType.Text).ToList();
        }

        public virtual List<BetItem> NeedSetBetItemList(string accountName, string accountPos, byte tableno)
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("SELECT  * FROM Bets WHERE AccountName = '" + accountName + "' and AccountPos = '" + accountPos + "' and tableno = " + tableno.ToString() + " and BetStatus = 0 and datediff(ss,BetTime,getdate()) < 15 and BetTime < dateadd(ss,-2,getdate()) Order By BetID ");

            return SqlGateway.CreateInstance().Select<BetItem>(sqlstring.ToString(), CommandType.Text).ToList();
        }

        public virtual BetItem GetBetItem()
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("SELECT * FROM dbo.[Bets]");

            return SqlGateway.CreateInstance().SelectSingle<BetItem>(sqlstring.ToString(), CommandType.Text);
        }

        public virtual void SetStatus(int betID, Byte betStatus)
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("UPDATE Bets SET BetStatus = " + betStatus );
            if (betStatus == 2) sqlstring.Append(", SetTime = getdate() ");
            sqlstring.Append(" WHERE BetID = " + betID.ToString());

            SqlGateway.CreateInstance().ExecuteNonQuery(sqlstring.ToString(), CommandType.Text);
        }

        public virtual void SetStatus(int betID, Byte betStatus, string betResult, Decimal betProfit)
        {
            StringBuilder sqlstring = new StringBuilder();
            sqlstring.Append("UPDATE Bets SET BetStatus = " + betStatus + ", BetResult = '" + betResult + "', BetProfit = " + betProfit.ToString() + " ");
            sqlstring.Append(" WHERE BetID = " + betID.ToString());

            SqlGateway.CreateInstance().ExecuteNonQuery(sqlstring.ToString(), CommandType.Text);
        }
    }
}
